

var roomInfo={};
var gameDate={};
function run(io){
	io.on('connection', function(socket){
//	  console.log(socket.id+' connected11');
	  socket.on('join',function(data){
//	  	console.log(data);
//	  	console.log(roomInfo);
	  	if(data.creat){
	  		//创建房间
	  		if(!roomInfo[data.roomID]){
	  			roomInfo[data.roomID]=[];
	  			roomInfo[data.roomID].push(data.name);
	  			socket.join(data.roomID);
	  			io.to(socket.id).emit('msg',{flag:true,msg:'创建成功'});
	  			io.to(socket.id).emit('userin',{users:roomInfo[data.roomID]});
	  		}else{
	  			io.to(socket.id).emit('msg',{falg:false,msg:'已有该房间'});
	  		}
	  		
	  	}else{
	  		//加入房间
	  		if(roomInfo[data.roomID]){
	  			socket.join(data.roomID);
	  			roomInfo[data.roomID].push(data.name);
	  			//当socket.io版本大于1.0.x时，我们应该使用 io.to(socketid).emit('String', data) 来替代。
	  			io.to(socket.id).emit('msg1',{flag:true,name:data.name});
	  			io.to(data.roomID).emit('userin',{users:roomInfo[data.roomID]});//通知组内成员
	  		}else{
	  			io.to(socket.id).emit('msg1',{flag:false,msg:'没有该房间号'});
	  		}
	  	}
	  	
	  });
	   socket.on('done',function(dt){
	   		socket.broadcast.emit("done",{data:dt.data});
//	   		gameDate['room'+dt.room]={};
//	   		gameDate['room'+dt.room].gameStart=true;
//	   		gameDate['room'+dt.room].data=dt.data;
//	   		console.log(gameDate);
	   })
//	   socket.on('touchGameData',function(room){
//	   	console.log('touch room',room);
//	   		var roomdt=gameDate['room'+room];
//	   		console.log('roomdt',roomdt);
//	   		io.to(socket.id).emit('getGameData',{data:roomdt});
//	   })
	  socket.on('start',function(){
	   		socket.broadcast.emit("start"); 
	   })
	  socket.on('end',function(dt){
	  	socket.broadcast.emit("end",dt); 
	  })
	  socket.on('miss',function(dt){
//	  	console.log('miss');
	  	var curroom=roomInfo['room'+dt.room];
	  	if(curroom){
	  		var index=curroom.indexOf(dt.name);
		  	roomInfo['room'+dt.room].splice(index,1);
		  	io.to('room'+dt.room).emit('userin',{users:roomInfo['room'+dt.room]});//通知组内成员
	  	}
	  	
	  })
	  socket.on('closealert',function(){
	  	socket.broadcast.emit('closealert');
	  })
	});
}
module.exports.run=run;